package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.FormatUtil;
import com.ruoyi.system.business.QsTagImportBusinessService;
import com.ruoyi.system.business.vo.QstagModbusTcp;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.service.IAlarmtypeService;
import com.ruoyi.system.service.IRegService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.service.IQstagService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * 变量寄存器配置Service业务层处理
 * 
 * @author yaozh
 * @date 2024-02-24
 */
@Service
public class QstagServiceImpl implements IQstagService 
{
    private static final Logger log = LoggerFactory.getLogger(QstagServiceImpl.class);
    @Autowired
    private QstagMapper qstagMapper;
    @Autowired
    private IRegService regService;
    @Autowired
    private IAlarmtypeService alarmtypeService;
    @Resource
    private DrinfoMapper drinfoMapper;
    @Resource
    private DrtypemodeMapper drtypemodeMapper;
    @Resource
    private RegGroupMapper regGroupMapper;
    @Resource
    private RegMapper regMapper;
    @Resource
    private QsTagImportBusinessService qsTagImportBusinessService;

    @Override
    public List<Qstag> findAll(String dbName){
        List<Qstag> list = qstagMapper.findAll(dbName);
        return list;
    }

    /**
     * 根据tagName的中文名称（tagNameCN字段）进行等值查询
     * @param dbName
     * @param tagNameCN
     */
    public Qstag findObjectByTagNameCnEquals(String dbName, String tagNameCN){
        return qstagMapper.findObjectByTagNameCnEquals(dbName,tagNameCN);
    }

    public List<Qstag> findAllQsTagOfTagNameCN(String dbName){
        return qstagMapper.findAllQsTagOfTagNameCN(dbName);
    }

    @Override
    public List<Qstag> findAllByTagNameCNLike(String dbName, String tagName) {
        return qstagMapper.findAllByTagNameCNLike(dbName,tagName);
    }

    @Override
    public List<Qstag> findAllByItemDrIdAndItemId(String dbName, Integer itemDrId, String itemId) {
        List<Qstag> list = qstagMapper.findAllByItemDrIdAndItemId(dbName, itemDrId, itemId);
        return list;
    }

    /**
     * 根据tagName进行模糊查询
     * @param dbName
     * @param tagName
     * @return
     */
    @Override
    public List<Qstag> findAllByTagNameLike(String dbName, String tagName) {
        return qstagMapper.findAllByTagNameLike(dbName,tagName);
    }

    @Override
    public int updateFormulaByTagName(String dbName, Qstag qstag) {
        int row = 0;
        try {
            row = qstagMapper.updateFormulaByTagName(dbName, qstag);//更新寄存器变量的公式
        } catch (Exception e) {
            e.printStackTrace();
            //throw new ServiceException("更新失败");
        }
        return row;
    }

    @Override
    public int updateFormula(String dBname, Qstag qstag) {
        int row = 0;
        try {
            row = qstagMapper.updateFormula(dBname, qstag);//更新寄存器变量的公式
        } catch (Exception e) {
            e.printStackTrace();
            //throw new ServiceException("更新失败");
        }
        return row;
    }

    /**
     * 根据tagName进行等值查询
     * @param dbName
     * @param tagName
     * @return
     */
    public Qstag findObjectByTagNameEquals(String dbName, String tagName){
        return qstagMapper.findObjectByTagNameEquals(dbName,tagName);
    }

    /**
     * 查询变量寄存器配置
     * 
     * @param tagid 变量寄存器配置主键
     * @return 变量寄存器配置
     */
    @Override
    public Qstag selectQstagByTagid(Long tagid,String dbName)
    {
        return qstagMapper.selectQstagByTagid(tagid,dbName);
    }

    @Override
    public List<Qstag> selectQstagByTagidList(Long[] list, String dbName){
        return qstagMapper.findObjectByTagIds(list,dbName);
    }

    /**
     * 查询变量寄存器配置列表
     * 
     * @param qstag 变量寄存器配置
     * @return 变量寄存器配置
     */
    @Override
    public List<Qstag> selectQstagList(Qstag qstag,String dbName)
    {
        List<Qstag> list = qstagMapper.selectQstagList(qstag,dbName);
        if(list != null && list.size() > 0){
            Map<String,List<Alarmtype>> map = new HashMap<>();
            List<Alarmtype> tempTypeList = alarmtypeService.selectAlarmtypeForAll(dbName);
            if(tempTypeList != null && tempTypeList.size() > 0){
                map = tempTypeList.stream().collect(Collectors.groupingBy(Alarmtype::getAlarmtypelevel));
            }
            for(Qstag tempQsTag : list){
                Long alarmLevel = tempQsTag.getAlarmlevel();
                Long hhAlarmLevel = tempQsTag.getHhAlarmLevel();
                Long hAlarmLevel = tempQsTag.getHalarmLevel();
                Long lAlarmLevel = tempQsTag.getLalarmLevel();
                Long llAlarmLevel = tempQsTag.getLlAlarmLevel();
                if(alarmLevel != null && alarmLevel >=0){
                    List<Alarmtype> alarmTypeList = map.get(String.valueOf(alarmLevel));
                    if(alarmTypeList != null && alarmTypeList.size() > 0){
                        tempQsTag.setAlarmtypeExpain(alarmTypeList.get(0).getAlarmtypeExpain());
                    }
                }
                if(hhAlarmLevel != null && hhAlarmLevel >=0){
                    List<Alarmtype> alarmTypeList = map.get(String.valueOf(hhAlarmLevel));
                    if(alarmTypeList != null && alarmTypeList.size() > 0){
                        tempQsTag.setHhAlarmTypeExplain(alarmTypeList.get(0).getAlarmtypeExpain());
                    }
                }
                if(hAlarmLevel != null && hAlarmLevel >=0){
                    List<Alarmtype> alarmTypeList = map.get(String.valueOf(hAlarmLevel));
                    if(alarmTypeList != null && alarmTypeList.size() > 0){
                        tempQsTag.setHalarmTypeExplain(alarmTypeList.get(0).getAlarmtypeExpain());
                    }
                }
                if(lAlarmLevel != null && lAlarmLevel >=0){
                    List<Alarmtype> alarmTypeList = map.get(String.valueOf(lAlarmLevel));
                    if(alarmTypeList != null && alarmTypeList.size() > 0){
                        tempQsTag.setLalarmTypeExplain(alarmTypeList.get(0).getAlarmtypeExpain());
                    }
                }
                if(llAlarmLevel != null && llAlarmLevel >=0){
                    List<Alarmtype> alarmTypeList = map.get(String.valueOf(llAlarmLevel));
                    if(alarmTypeList != null && alarmTypeList.size() > 0){
                        tempQsTag.setLlAlarmTypeExplain(alarmTypeList.get(0).getAlarmtypeExpain());
                    }
                }
            }
        }
        return list;
    }

    @Override
    public List<Qstag> selectHomepageQsTagList(String dbName){
        List<Qstag> list = qstagMapper.selectHomepageQsTagList(dbName);
        return list;
    }

    /**
     * 新增变量寄存器配置
     * 
     * @param qstag 变量寄存器配置
     * @return 结果
     */
    @Override
    public int insertQstag(Qstag qstag,String dbName)
    {
        return qstagMapper.insertQstag(qstag,dbName);
    }

    /**
     * 修改变量寄存器配置
     * 
     * @param qstag 变量寄存器配置
     * @return 结果
     */
    @Override
    @Transactional
    public int updateQstag(Qstag qstag,String dbName)
    {
        Long boolType = qstag.getBoolType();
        if(boolType != null && boolType == 0){
            //当 非布尔变量 来处理时候，需要清空 布尔变量 的那些项，防止误输入误选择而导致错误
            qstag.setAlarmlevel(null);//清空
        }else{
            //当 布尔变量 来处理时候，需要清空 非布尔变量 的那些项，防止误输入误选择而导致错误
            qstag.setHhValue(null);
            qstag.setHhAlarmLevel(null);
            qstag.setHvalue(null);
            qstag.setHalarmLevel(null);
            qstag.setLvalue(null);
            qstag.setLalarmLevel(null);
            qstag.setLlValue(null);
            qstag.setLlAlarmLevel(null);
        }
        int row = qstagMapper.updateQstag(qstag,dbName);
        if(row > 0 && boolType != null && boolType == 1){
            //更新reg表的
            String tagName = qstag.getTagname();
            String tagNameCN = qstag.getTagnameCN();
            Long alarmLevel = qstag.getAlarmlevel();
            String alarmLevelStr = "0";
            String isAlarm = "0";
            if(alarmLevel != null && alarmLevel > 0){
                isAlarm = "1";
                alarmLevelStr = String.valueOf(alarmLevel);
            }
            if(StringUtils.hasLength(tagName)){
                regService.updateRegAlarmByTagName(tagName,isAlarm,alarmLevelStr,dbName);
            }
        }
        return row;
    }

    /**
     * 批量删除变量寄存器配置
     * 
     * @param tagids 需要删除的变量寄存器配置主键
     * @return 结果
     */
    @Override
    public int deleteQstagByTagids(Long[] tagids,String dbName)
    {
        return qstagMapper.deleteQstagByTagids(tagids,dbName);
    }

    /**
     * 删除变量寄存器配置信息
     * 
     * @param tagid 变量寄存器配置主键
     * @return 结果
     */
    @Override
    public int deleteQstagByTagid(Long tagid,String dbName)
    {
        return qstagMapper.deleteQstagByTagid(tagid,dbName);
    }

    @Override
    public String importQsTag(List<Qstag> qsTagList, Boolean isUpdateSupport, String operName, String dbName){
        if (com.ruoyi.common.utils.StringUtils.isNull(qsTagList) || qsTagList.size() == 0)
        {
            throw new ServiceException("导入项目点表配置数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (Qstag qsTag : qsTagList)
        {
            try{
                // 验证是否存在
                String itemId = qsTag.getItemid();
                List<Qstag> tempQsTagList = qstagMapper.selectQstagByItemId(qsTag.getItemid(),dbName);
                if (tempQsTagList == null || tempQsTagList.size() == 0)
                {
                    qsTag.setCreateBy(operName);
                    String formula = qsTag.getTagformula();
                    if(StringUtils.hasLength(formula)){
                        formula = formula.trim();
                        qsTag.setTagformula(formula);
                    }
                    qstagMapper.insertQstag(qsTag,dbName);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、寄存器名称 " + qsTag.getTagnameCN() + "、寄存器地址 " + qsTag.getItemid() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    Qstag updateQsTag = null;
                    if(StringUtils.hasLength(itemId) && "local".equals(itemId.toLowerCase())){
                        //存在多个
                        for(Qstag tempQsTag : tempQsTagList){
                            if(tempQsTag != null && ((StringUtils.hasLength(tempQsTag.getTagname()) && tempQsTag.getTagname().equals(qsTag.getTagname())) || (StringUtils.hasLength(tempQsTag.getTagnameCN()) && tempQsTag.getTagnameCN().equals(qsTag.getTagnameCN())))){
                                updateQsTag = tempQsTag;
                                break;
                            }
                        }
                    }else{
                        //只存在一条记录
                        updateQsTag = tempQsTagList.get(0);
                    }
                    if(updateQsTag != null && updateQsTag.getTagid() != null){
                        qsTag.setTagid(updateQsTag.getTagid());
                        String formula = qsTag.getTagformula();
                        if(StringUtils.hasLength(formula)){
                            formula = formula.trim();
                            qsTag.setTagformula(formula);
                        }
                        qsTag.setUpdateBy(operName);
                        qstagMapper.updateQstag(qsTag,dbName);
                        successNum++;
                        successMsg.append("<br/>" + successNum + "、寄存器名称 " + qsTag.getTagnameCN() + "、寄存器地址 " + qsTag.getItemid() + " 更新成功");
                    }else{
                        qsTag.setCreateBy(operName);
                        qstagMapper.insertQstag(qsTag,dbName);
                        successNum++;
                        successMsg.append("<br/>" + successNum + "、寄存器名称 " + qsTag.getTagnameCN() + "、寄存器地址 " + qsTag.getItemid() + " 导入成功");
                    }
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、寄存器名称 " + qsTag.getTagnameCN() + "、寄存器地址 " + qsTag.getItemid() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、寄存器名称 " + qsTag.getTagnameCN() + "、寄存器地址 " + qsTag.getItemid() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }

        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    public String importQsTagInit(List<QstagModbusTcp> qsTagList, Boolean isUpdateSupport, String operName, String dbName){
        if (com.ruoyi.common.utils.StringUtils.isNull(qsTagList) || qsTagList.size() == 0)
        {
            throw new ServiceException("导入项目点表配置数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();

        //导入处理
        AjaxResult ajaxResult = qsTagImportBusinessService.handlerImportModbusTcpExcel(dbName,qsTagList);

        if (ajaxResult != null && !ajaxResult.isSuccess())
        {
            failureMsg.append("<br/>" + ajaxResult.toString());
            failureMsg.insert(0, "很抱歉，导入失败！错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.append("<br/>" + ajaxResult.toString());
            successMsg.insert(0, "恭喜您，数据已全部导入成功！结果如下：");
        }
        return successMsg.toString();
    }

    @Transactional
    @Override
    public void importObjectsByImport(String dbName, List<Qstag> addList, List<Qstag> deleteList) {
        //获取分组信息
        List<RegGroup> groupList = regGroupMapper.findAllRegGroup(dbName);//获取项目分组表reg_group
        Map<String,RegGroup> groupMap = new HashMap<>();
        if(groupList != null){
            groupList.forEach(regGroup -> {
                groupMap.put(String.valueOf(regGroup.getGroupName()),regGroup);
            });
        }

        //获取阈值信息
        //List<Subinfo> subInfoAllList = subinfoService.findAllObject(dbName);
        //Map<String,Integer> regSubInfoMap = subinfoService.getRegSubInfoMapList(dbName,subInfoAllList);//阈值ID，map

        //获取设备信息列表
        Map<String,List<Drinfo>> drInfoAllMap = new HashMap<>();
        Map<Long,String> drInfoMap = new HashMap<>();
        List<Drinfo> drInfoAllList = drinfoMapper.findAllDrinfo(dbName);
        if(drInfoAllList != null && drInfoAllList.size() > 0){
            drInfoAllMap = drInfoAllList.stream().collect(Collectors.groupingBy(Drinfo::getDrcode));//根据设备编码分组
            drInfoAllList.forEach(drInfo -> {
                drInfoMap.put(drInfo.getDrid(),drInfo.getDrcode());
            });
        }

        int rows = 0;
        Integer tagId = null;
        for (Qstag tag : addList) {
            String tagName = tag.getTagname();
            String tagNameCN = tag.getTagnameCN().trim().replaceAll("\n", "").replaceAll("\r", "");
            String itemId = tag.getItemid();
            tag.setTagnameCN(tagNameCN);
            Qstag oldQsTag = null;
            if("local".equals(itemId)){
                oldQsTag = qstagMapper.findObjectByTagNameEquals(dbName, tagName);
            }else{
                try{
                    oldQsTag = qstagMapper.findObjectByItemId(dbName, itemId);//可能存在多个itemId的记录，这里返回单个对象，可能发生异常
                }catch (Exception e){
                    log.error("qstagMapper.findObjectByItemId Exception itemId:" + itemId,e);
                }
            }
            Boolean isInsert = null;
            if(oldQsTag == null){
                isInsert = true;
            }else{
                isInsert = false;
            }
            if(isInsert){
                //插入，新增点表
                try{
                    qstagMapper.insertObject(dbName, tag);
                }catch (Exception e){
                    log.error("qstagMapper.insertObject Exception",e);
                }
            }else{
                //更新，修改点表
                try{
                    tag.setTagid(oldQsTag.getTagid());
                    qstagMapper.updateObjectByTagIdForImport(dbName, tag);
                }catch (Exception e){
                    log.error("qstagMapper.updateObjectByTagIdForImport Exception",e);
                }
            }

            //检查reg表
            String[] tagNameArray = tagNameArray(tagNameCN);
            String drCode = tagNameArray[0];//分离出设备编码
            String regName = tagNameArray[1];//分离出设备属性
            Long drId = null;
            Long drTypeId = null;
            if(StringUtils.hasLength(drCode)){
                List<Drinfo> drInfoList = drInfoAllMap.get(drCode);//根据设备编码获取设备信息
                if(drInfoList != null && drInfoList.size() > 0){
                    drId = drInfoList.get(0).getDrid();//找到设备ID
                    drTypeId = drInfoList.get(0).getDrtypeid();//找到设备类型ID
                }
            }

            //如果是新增点表的情况，也需要新增对应的属性，如果是已经存在，则不需要新增
            List<Drtypemode> drTypeModes = new ArrayList<>();
            Reg oldReg = regMapper.findRegByDrIdAndRegName(dbName,drId,regName);
            if(drTypeId != null && StringUtils.hasLength(regName)){
                //如果知道所属设备类型（通常是执行更新点表时候会执行到这里），并且设备属性不为空
                List<Drtypemode> drTypeModeList = drtypemodeMapper.findAllObjectByDrTypeId(dbName, drTypeId);//去项目库获取设备属性
                if(drTypeModeList != null && drTypeModeList.size() > 0){
                    for(Drtypemode drTypeMode : drTypeModeList){
                        String oldRegName = drTypeMode.getRegName();
                        if(regName.equals(oldRegName)){//如果新增的点表属性的名称已经在模板里面
                            if(!regName.contains("电量")){
                                drTypeMode.setTagName(tagName);
                            }
                            drTypeMode.setIsThreeDShow(1);

                            if(oldReg != null){
                                //reg表中已经存在，则只更新一些数据
                                oldReg.setRegUnits(drTypeMode.getRegUnits());
                                if(!regName.contains("电量")){
                                    oldReg.setTagName(tagName);
                                }
                                oldReg.setRegType(drTypeMode.getRegType());
                                oldReg.setRegReadwrite(drTypeMode.getRegReadwrite());
                                //oldReg.setTagValue(drTypeMode.getTagValue());//不需要修改这个tagValue
                                oldReg.setRegGroupId(drTypeMode.getRegGroupId());//
                                oldReg.setRegListshowlevel(String.valueOf(drTypeMode.getRegListshowlevel()));//不需要修改这个
                                oldReg.setRegSub(drTypeMode.getRegSub());//设置需要显示的回显字符
                                oldReg.setRegDrshowtype(drTypeMode.getRegDrshowtype());
                                String isListShow = drTypeMode.getIslistshow();
                                long isShow = 1L;
                                if(StringUtils.hasLength(isListShow)){
                                    try{
                                        isShow = Long.parseLong(isListShow);
                                    }catch (Exception e){
                                        e.printStackTrace();
                                    }
                                }
                                oldReg.setIsThreedShow(isShow);
                                oldReg.setIsHistory(drTypeMode.getIshistory());
                                oldReg.setIsEnergy(drTypeMode.getIsenergy());
                                oldReg.setIsAlarm(drTypeMode.getIsalarm());
                                oldReg.setTagTime(drTypeMode.getTagTime());
                                regMapper.updateObject(dbName,oldReg);
                            }else{
                                //reg表中不存在，但是模板里面有，而且点表也有，则往reg中新增数据
                                drTypeModes.add(drTypeMode);
                            }
                            break;
                        }else{
                            //如果新增的属性不在模板里面，则需要往模板添加对应的点表属性，前面导入检查的时候已经添加了
                            //do nothing，所以这里应该不会执行到
                        }
                    }
                }
            }

            //新增reg的，更新点表的时候，顺便更新设备属性展示表，这样展示的数据如果有缺少就补回来了
            if(drTypeModes.size() > 0){
                regMapper.insertObjectByDrtypemodes(dbName,drId,drTypeModes);
            }
        }

        //删除的点表
        for(Qstag tag : deleteList){
            //删除qstag里面的
            String tagNameCN = tag.getTagnameCN().trim().replaceAll("\n", "").replaceAll("\r", "");
            String itemId = tag.getItemid();
            String tagName = null;
            if(StringUtils.hasLength(itemId)){
                Qstag tempQsTag = qstagMapper.findObjectByItemId(dbName,itemId);
                if(tempQsTag != null){
                    tagName = tempQsTag.getTagname();
                    qstagMapper.deleteObjectByItemId(dbName,itemId);//删除非'local'类型的，'local'类型的不能删除
                }
            }

            //删除reg表里面的
            String[] tagNameArray = tagNameArray(tagNameCN);
            String drCode = tagNameArray[0];
            String regName = tagNameArray[1];
            Long drId = null;
            Long drTypeId = null;
            if(StringUtils.hasLength(drCode)){
                Drinfo drinfo = drinfoMapper.findDrByDrCode(dbName,drCode);
                if(drinfo != null){
                    drId = drinfo.getDrid();
                    drTypeId = drinfo.getDrtypeid();
                }
            }else if(StringUtils.hasLength(tagName)){
                //如果drCode为空，则根据tagName删除
                List<Reg> list = regMapper.findRegByTagName(dbName,tagName);
                if(list != null && list.size() > 0){
                    for(Reg reg : list){
                        String tempRegName = reg.getRegName();
                        //String groupId = reg.getRegGroupId();
                        if(StringUtils.hasLength(tagNameCN) && tagNameCN.equals(tempRegName) && regName.equals(tempRegName)){
                            drId = reg.getDrId();
                            break;
                        }
                    }
                }
            }
            if(drId != null && StringUtils.hasLength(regName)){
                regMapper.deleteObjectByDrIdAndRegName(dbName,drId,regName);
            }
        }

        //检查reg表，删除reg表中多余数据
        List<Reg> regList = regMapper.findAllReg(dbName);
        if(regList != null){
            regList.forEach(reg -> {
                Long drId = reg.getDrId();
                String regName = reg.getRegName();
                String drCode = drInfoMap.get(drId);
                if(StringUtils.hasLength(drCode) && StringUtils.hasLength(regName)){
                    String firstChar = regName.substring(0,1);
                    boolean isChinese = FormatUtil.isContainChinese(firstChar);
                    String tagNameCN = drCode + (isChinese ? "" : "-") + regName;//格式如CH远程，或CH1-A电压
                    Qstag qstag = qstagMapper.findObjectByTagNameCnEquals(dbName,tagNameCN);
                    if(qstag == null){
                        List<Long> subRegIdList = new ArrayList<>();
                        subRegIdList.add(reg.getRegId());
                        if(!tagNameCN.contains("电量")){
                            regMapper.updateIsThreeDShowByRegIdList(dbName,subRegIdList,0);//不删除，改为不显示即可
                        }
                    }else{
                        log.info("设置{}显示",tagNameCN);
                    }
                }
            });
        }
    }

    private String[] tagNameArray(String tagNameCN){
        String prefix = getPrefixOfCharAndNumber(tagNameCN);
        if(StringUtils.hasLength(prefix) && prefix.contains("-")){
            prefix = prefix.substring(0,prefix.indexOf("-"));
        }
        String regName = tagNameCN.substring(prefix.length());
        if(regName.contains("-") && regName.startsWith("-")){
            regName = regName.substring(1);
        }
        String[] tagNameArray = new String[2];
        tagNameArray[0] = prefix;
        tagNameArray[1] = regName;
        return tagNameArray;
    }

    public static String getPrefixOfCharAndNumber(String name){
        String patternStr = "\\d+.\\d+|\\w+";
        return getCharAndNumberFromStr(name,"-",patternStr);
    }

    public static String getNumber(String name){
        String patternStr = "(\\d+\\.\\d+)|(\\d+)";
        return getCharAndNumberFromStr(name,"",patternStr);
    }

    public static String getCharAndNumberFromStr(String name,String split,String patternStr){
        Pattern pattern = Pattern.compile(patternStr);
        Matcher ma = pattern.matcher(name);
        StringBuilder sb = new StringBuilder();
        while(ma.find()){
            sb.append(ma.group() + split);
        }
        return sb.toString();
    }
}
